<?php
/*
 * 	Yamp52 - Yet Another Magical PHP framework
 *	http://code.google.com/p/yamp52/
 *	
 *	Copyright (C) 2009, Krzysztof Drezewski <krzych@krzych.eu>
 *	
 *	This program is free software; you can redistribute it and/or modify
 *	it under the terms of the GNU General Public License as published by
 *	the Free Software Foundation; either version 3 of the License, or
 *	(at your option) any later version.
 *	
 *	This program is distributed in the hope that it will be useful,
 *	but WITHOUT ANY WARRANTY; without even the implied warranty of
 *	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 *	GNU General Public License for more details.
 *	
 *	You should have received a copy of the GNU General Public License
 *	along with this program. If not, see <http://www.gnu.org/licenses/>.
 */


class Fixture {

	/**
	 * Mandatory method which will populate freshly created database
	 * tables with objects (minimal configuration)
	 *
	 */
	public static function populate() {

		Doctrine_Query::create()->delete('Acl')->execute();
		Doctrine_Query::create()->delete('Role')->execute();
		Doctrine_Query::create()->delete('RoleAcl')->execute();
		Doctrine_Query::create()->delete('RoleSecuredObject')->execute();
		Doctrine_Query::create()->delete('SecuredObject')->execute();
		Doctrine_Query::create()->delete('User')->execute();
		Doctrine_Query::create()->delete('UserAcl')->execute();
		Doctrine_Query::create()->delete('UserRole')->execute();

		$Anon = new Role();
		$Anon->name = 'ANONYMOUS';

		$AclRolowa = new Acl();
		$AclRolowa->name = 'ACL_ROLOWA';
		$AclRolowa->save();

		$Anon->Acls[] = $AclRolowa;

		$secur = array(
		array('MainFlowController','defaultAction'),
		array('AuthenticateFlowController','loginAction'),
		array('AuthenticateFlowController','dologinAction'),
		array('AuthenticateFlowController','dologinValidate'),
		array('AuthenticateFlowController','logoutAction'),
		array('AuthenticateFlowController','nopermAction')
		);

		foreach($secur as $idx => $sec) {
			$Secured = new SecuredObject();
			$Secured->class = $sec[0];
			$Secured->method = $sec[1];
			$Secured->allowed = true;
			$Secured->save();

			$Anon->SecuredObjects[] = $Secured;
		}

		$Anon->save();


		$treeObject = Doctrine::getTable('Role')->getTree();
		$treeObject->createRoot($Anon);

		$User = new Role();
		$User->name = 'USER';
		$User->getNode()->insertAsLastChildOf($Anon);

		$AdvancedUser = new Role();
		$AdvancedUser->name = 'ADVANCED_USER';
		$AdvancedUser->getNode()->insertAsLastChildOf($User);

		$Engineer = new Role();
		$Engineer->name = 'ENGINEER';
		$Engineer->getNode()->insertAsLastChildOf($AdvancedUser);

		$Admin = new Role();
		$Admin->name = 'ADMIN';
		$Admin->getNode()->insertAsLastChildOf($Engineer);

		$Salesman = new Role();
		$Salesman->name = 'SALESMAN';
		$Salesman->getNode()->insertAsLastChildOf($AdvancedUser);

		$AclGlowna = new Acl();
		$AclGlowna->name = 'ACL_MAIN';
		$AclGlowna->save();

		$AclWazna = new Acl();
		$AclWazna->name = 'ACL_IMPORTANT';
		$AclWazna->save();

		$Test = new User();
		$Test->firstname = 'Testoslav';
		$Test->lastname = 'Tescinsky';
		$Test->login = 'test';	//our user login
		$Test->active = true;
		$Test->password = crypt('test');	//and password

		$Test->Roles[] = $Engineer;
		$Test->Roles[] = $Admin;

		$Test->Acls[] = $AclGlowna;
		$Test->Acls[] = $AclWazna;

		$Test->save();

		$secur = array(
		array('TestFlowController','actionValidate'),
		array('TestFlowController','actionAction'),
		array('TestFlowController','anotherAction'),
		);

		foreach($secur as $idx => $sec) {
			$Secured = new SecuredObject();
			$Secured->class = $sec[0];
			$Secured->method = $sec[1];
			$Secured->allowed = true;
			$Secured->save();

			$Admin->SecuredObjects[] = $Secured;
		}

		$Admin->save();

	}

}